//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.wcp.controller;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.farm.core.auth.domain.LoginUser;
import com.farm.core.page.ViewMode;
import com.farm.doc.dao.FarmRfDoctypeDaoInter;
import com.farm.doc.domain.FarmDocfile;
import com.farm.doc.domain.FarmDocruninfo;
import com.farm.doc.domain.FarmDoctext;
import com.farm.doc.domain.FarmDoctype;
import com.farm.doc.domain.ex.DocEntire;
import com.farm.doc.exception.CanNoReadException;
import com.farm.doc.exception.DocNoExistException;
import com.farm.doc.server.FarmDocIndexInter;
import com.farm.doc.server.FarmDocManagerInter;
import com.farm.doc.server.FarmDocOperateRightInter;
import com.farm.doc.server.FarmDocRelationManagerInter;
import com.farm.doc.server.FarmDocRunInfoInter;
import com.farm.doc.server.FarmDocgroupManagerInter;
import com.farm.doc.server.FarmDocmessageManagerInter;
import com.farm.doc.server.FarmFileIndexManagerInter;
import com.farm.doc.server.FarmFileManagerInter;
import com.farm.wcp.know.service.KnowServiceInter;
import com.farm.wcp.util.ThemesUtil;
import com.farm.web.WebUtils;

@RequestMapping({"/webdoc"})
@Controller
public class DocController extends WebUtils {
    private static final Logger log = Logger.getLogger(DocController.class);
    @Resource
    private FarmDocgroupManagerInter farmDocgroupManagerImpl;
    @Resource
    private FarmFileManagerInter farmFileManagerImpl;
    @Resource
    private FarmDocManagerInter farmDocManagerImpl;
    @Resource
    private FarmDocRunInfoInter farmDocRunInfoImpl;
    @Resource
    private KnowServiceInter KnowServiceImpl;
    @Resource
    private FarmDocmessageManagerInter farmDocmessageManagerImpl;
    @Resource
    private FarmDocOperateRightInter farmDocOperateRightImpl;
    @Resource
    private FarmDocIndexInter farmDocIndexManagerImpl;
    @Resource
    private FarmRfDoctypeDaoInter farmRfDoctypeDaoImpl;
    @Resource
    private FarmFileIndexManagerInter farmFileIndexManagerImpl;
    @Resource
    private FarmDocRelationManagerInter farmDocrelationManagerImpl;

    public DocController() {
    }

    @RequestMapping(
        value = {"/view/Pub{docid}"},
        method = {RequestMethod.GET}
    )
    public ModelAndView showDoc(@PathVariable("docid") String docid, HttpSession session, HttpServletRequest request) throws Exception {
        ViewMode page = ViewMode.getInstance();

        try {
            DocEntire doc = this.farmDocManagerImpl.getDoc(docid, this.getCurrentUser(session));
            page.putAttr("DOCE", doc);
            List<FarmDoctext> versions = this.farmDocManagerImpl.getDocVersions(docid);
            page.putAttr("VERSIONS", versions);
            if (this.getCurrentUser(session) != null) {
                boolean isenjoy = this.farmDocRunInfoImpl.isEnjoyDoc(this.getCurrentUser(session).getId(), docid);
                page.putAttr("ISENJOY", isenjoy);
            }

            FarmDoctype type = doc.getType();
            page.putAttr("TYPEID", type == null ? "" : type.getId());
            Set<String> fileTypes = new HashSet();
            Iterator var10 = doc.getFiles().iterator();

            while(var10.hasNext()) {
                FarmDocfile node = (FarmDocfile)var10.next();
                fileTypes.add(node.getExname().trim().replace(".", "").toUpperCase());
            }

            page.putAttr("FILETYPES", fileTypes);
            this.farmDocRunInfoImpl.visitDoc(docid, this.getCurrentUser(session), this.getCurrentIp(request));
            LoginUser user = this.getCurrentUser(session);
            List relationdocs;
            if (type != null) {
                relationdocs = this.farmDocRunInfoImpl.getTypeDocs(type == null ? "" : type.getId(), user == null ? "none" : user.getId(), 10);
                page.putAttr("TYPEDOCS", relationdocs);
            }

            if (doc.getDoc().getDomtype().equals("1")) {
                relationdocs = this.farmDocrelationManagerImpl.getDocRelationDoc(docid);
                page.putAttr("RELATIONDOCS", relationdocs);
                return page.returnModelAndView(ThemesUtil.getThemePath() + "/know/view");
            }

            if (doc.getDoc().getDomtype().equals("5")) {
                return page.returnModelAndView(ThemesUtil.getThemePath() + "/webfile/webfile");
            }

            if (doc.getDoc().getDomtype().equals("4")) {
                if (doc.getGroup() != null && doc.getGroup().getId() != null) {
                    return page.putAttr("groupid", doc.getGroup().getId()).returnRedirectUrl("/webgroup/Pubshow.do");
                }

                return ViewMode.getInstance().setError("请检查该小组首页，的关联小组是否存在！").returnModelAndView(ThemesUtil.getThemePath() + "/error");
            }
        } catch (CanNoReadException var11) {
            return ViewMode.getInstance().setError(var11.toString()).returnModelAndView(ThemesUtil.getThemePath() + "/error");
        } catch (DocNoExistException var12) {
            return ViewMode.getInstance().setError(var12.toString()).returnModelAndView(ThemesUtil.getThemePath() + "/error");
        } catch (Exception var13) {
            var13.printStackTrace();
            return ViewMode.getInstance().setError(var13.toString()).returnModelAndView(ThemesUtil.getThemePath() + "/error");
        }

        return ViewMode.getInstance().setError("请实现正确的DOCTYPE类型解析").returnModelAndView(ThemesUtil.getThemePath() + "/error");
    }

    @RequestMapping(
        value = {"/view/PubFile{fileid}"},
        method = {RequestMethod.GET}
    )
    public ModelAndView showFile(@PathVariable("fileid") String fileid, String docid, HttpSession session, HttpServletRequest request) throws Exception {
        ViewMode page = ViewMode.getInstance();

        try {
            FarmDocfile file = this.farmFileManagerImpl.getFile(fileid);
            if (file == null) {
                throw new DocNoExistException();
            }

            file.setUrl(this.farmFileManagerImpl.getFileURL(file.getId()));
            page.putAttr("file", file);
            page.putAttr("docid", docid);
        } catch (DocNoExistException var7) {
            log.error(var7.getMessage());
            this.farmFileIndexManagerImpl.delFileLucenneIndex(fileid);
            return ViewMode.getInstance().setError(var7.toString()).returnModelAndView(ThemesUtil.getThemePath() + "/error");
        } catch (Exception var8) {
            log.error(var8.getMessage());
            return ViewMode.getInstance().setError(var8.toString()).returnModelAndView(ThemesUtil.getThemePath() + "/error");
        }

        return page.returnModelAndView(ThemesUtil.getThemePath() + "/webfile/file");
    }

    @RequestMapping({"/enjoy"})
    @ResponseBody
    public Map<String, Object> enjoy(HttpSession session, String id) {
        try {
            this.farmDocRunInfoImpl.enjoyDoc(this.getCurrentUser(session).getId(), id);
            return ViewMode.getInstance().putAttr("commitType", "0").returnObjMode();
        } catch (Exception var4) {
            return ViewMode.getInstance().putAttr("commitType", "1").setError(var4.getMessage()).returnObjMode();
        }
    }

    @RequestMapping({"/FLunEnjoy"})
    @ResponseBody
    public Map<String, Object> unenjoy(HttpSession session, String id) {
        try {
            this.farmDocRunInfoImpl.unEnjoyDoc(this.getCurrentUser(session).getId(), id);
            return ViewMode.getInstance().putAttr("commitType", "0").returnObjMode();
        } catch (Exception var4) {
            return ViewMode.getInstance().putAttr("commitType", "1").setError(var4.getMessage()).returnObjMode();
        }
    }

    @RequestMapping({"/PubVersion"})
    public ModelAndView showVersion(String textid, HttpSession session, HttpServletRequest request) {
        ViewMode page = ViewMode.getInstance();

        try {
            DocEntire doc = this.farmDocManagerImpl.getDocVersion(textid, this.getCurrentUser(session));
            if (!doc.getDoc().getState().equals("1")) {
                throw new RuntimeException("没有权限访问该文档");
            } else {
                List<FarmDoctext> versions = this.farmDocManagerImpl.getDocVersions(doc.getDoc().getId());
                page.putAttr("VERSIONS", versions);
                return page.putAttr("DOCE", doc).returnModelAndView(ThemesUtil.getThemePath() + "/know/version");
            }
        } catch (Exception var7) {
            return page.setError(var7.getMessage()).returnModelAndView(ThemesUtil.getThemePath() + "/error");
        }
    }

    @RequestMapping({"/FLflyKnow"})
    public ModelAndView flyKnow(String id, HttpSession session) {
        try {
            this.farmDocOperateRightImpl.flyDoc(id, this.getCurrentUser(session));
            return ViewMode.getInstance().returnRedirectUrl("/webdoc/view/Pub" + id + ".html");
        } catch (Exception var4) {
            return ViewMode.getInstance().setError(var4.toString()).returnModelAndView(ThemesUtil.getThemePath() + "/error");
        }
    }

    @RequestMapping({"/FLDelKnow"})
    public ModelAndView delCommit(String id, HttpSession session) {
        try {
            DocEntire doc = this.farmDocManagerImpl.deleteDoc(id, this.getCurrentUser(session));
            if (doc.getDoc().getDomtype().equals("5")) {
                Iterator var5 = doc.getFiles().iterator();

                while(var5.hasNext()) {
                    FarmDocfile file = (FarmDocfile)var5.next();
                    this.farmFileIndexManagerImpl.delFileLucenneIndex(file.getId());
                }
            }

            return ViewMode.getInstance().putAttr("MESSAGE", "知识&nbsp;'" + doc.getDoc().getTitle() + "'&nbsp;删除成功！").putAttr("TYPE", doc.getType()).returnModelAndView(ThemesUtil.getThemePath() + "/message");
        } catch (Exception var6) {
            log.error(var6.getMessage());
            return ViewMode.getInstance().setError(var6.toString()).returnModelAndView(ThemesUtil.getThemePath() + "/error");
        }
    }

    @RequestMapping({"/delImg"})
    public Map<String, Object> delImg(String imgid) {
        try {
            this.farmDocManagerImpl.delImg(imgid);
            return ViewMode.getInstance().returnObjMode();
        } catch (Exception var3) {
            return ViewMode.getInstance().setError(var3.toString()).returnObjMode();
        }
    }

    @RequestMapping({"/PubPraiseYes"})
    @ResponseBody
    public Map<String, Object> praiseYes(String id, HttpSession session, HttpServletRequest request) {
        try {
            if (this.getCurrentUser(session) == null) {
                this.farmDocRunInfoImpl.praiseDoc(id, request.getRemoteAddr());
            } else {
                this.farmDocRunInfoImpl.praiseDoc(id, this.getCurrentUser(session), request.getRemoteAddr());
            }

            FarmDocruninfo runinfo = this.farmDocRunInfoImpl.loadRunInfo(id);
            return ViewMode.getInstance().putAttr("runinfo", runinfo).returnObjMode();
        } catch (Exception var5) {
            var5.printStackTrace();
            return ViewMode.getInstance().setError(var5.toString()).returnObjMode();
        }
    }

    @RequestMapping({"/PubPraiseNo"})
    @ResponseBody
    public Map<String, Object> praiseNo(String id, HttpSession session, HttpServletRequest request) {
        try {
            if (this.getCurrentUser(session) == null) {
                this.farmDocRunInfoImpl.criticalDoc(id, this.getCurrentIp(request));
            } else {
                this.farmDocRunInfoImpl.criticalDoc(id, this.getCurrentUser(session), this.getCurrentIp(request));
            }

            FarmDocruninfo runinfo = this.farmDocRunInfoImpl.loadRunInfo(id);
            return ViewMode.getInstance().putAttr("runinfo", runinfo).returnObjMode();
        } catch (Exception var5) {
            var5.printStackTrace();
            return ViewMode.getInstance().setError(var5.toString()).returnObjMode();
        }
    }
}
